package com.yemast.readertesst

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.yemast.readertesst.model.BookDetail
import com.yemast.readertesst.model.MainPageData
import com.yemast.readertesst.model.PageInfo
import com.yemast.readertesst.view.BookDetailListAc
import kotlinx.android.synthetic.main.activity_main.*
import org.jsoup.Jsoup

class MainActivity : AppCompatActivity() {


    val tuijianadaptr: Tuijianadapter by lazy { Tuijianadapter() }
    var mDataList = mutableListOf<MainPageData>()

    var pageinfolist = mutableListOf<PageInfo>()


    lateinit var bookDetail: BookDetail
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        rl_view.layoutManager = LinearLayoutManager(this)
//        rl_view.addItemDecoration(GridSpacingItemDecoration())
        rl_view.adapter = tuijianadaptr

        tuijianadaptr.setOnItemClickListener { adapter, view, position ->
            Log.e("tag", "${System.currentTimeMillis()}");
            load {
                val doc = Jsoup.connect(mDataList[position].link).get()

                val corver = doc.getElementsByClass("block_img2")[0].getElementsByTag("img").attr("src")//封面
                val summary = doc.getElementsByClass("intro_info")[0].ownText()//简介
                val element = doc.getElementsByClass("block_txt2")[0]
                var auther = element.getElementsContainingOwnText("作者").text()//作者
                var sort = element.getElementsContainingOwnText("分类").text()//名字
                var status = element.getElementsContainingOwnText("状态").text()//作者
                var update = element.getElementsContainingOwnText("更新").text()//作者
                val newestpage = element.getElementsContainingOwnText("最新").text()//作者
                val newpageadress = element.getElementsContainingOwnText("最新")[0].getElementsByTag("a")[0].attr("abs:href")

                val elementsByTag = doc.getElementsByTag("option")//获取所有章节的分页列表
                elementsByTag.forEach {
                    val attr = it.attr("abs:value")
                    val doc2 = Jsoup.connect(attr).get()
                    val elementsContainingOwnText = doc2.getElementsContainingOwnText("正文")
                    elementsContainingOwnText[0].nextElementSibling().getElementsByTag("a").forEach {
                        ////获取分页内的具体章节
                        val pagelink = it.attr("abs:href")//正文地址
                        val pagename = it.text()//章节名称
                        pageinfolist.add(PageInfo(pagelink, pagename))

                    }
                }
                 bookDetail = BookDetail(mDataList[position].name, corver, summary, auther, sort, status, update, newestpage, newpageadress, pageinfolist)
            }.then {
                Log.e("tag", "${System.currentTimeMillis()}");
                val intent = Intent(this@MainActivity, BookDetailListAc::class.java)
                intent.putExtra("detail", bookDetail)


                startActivity(intent)

            }
        }

        tv_get_data.setOnClickListener {
            load {
                val doc = Jsoup.connect("https://m.bimo.cc").get();
                Log.e("tag", "");
                //获取首页推荐小说
                doc.getElementsByTag("li").forEach {
                    val name = it.getElementsByTag("a").text()//书的名字
                    val author = it.ownText().replace("/", "")//作者
                    val atag = it.getElementsByTag("a")
                    val id = atag.attr("href")
                    val link = atag.attr("abs:href")
                    Log.e("tag", "$link");
                    if (!author.isNullOrBlank()) {
                        mDataList.add(MainPageData(id, author, name, link))
                    }
                }
            }.then {
                Log.e("tag", "${mDataList.size}");
                tv_get_data.visibility = View.GONE
                rl_view.visibility = View.VISIBLE
                tuijianadaptr.setNewData(mDataList)

            }

        }

        button.setOnClickListener {
            startActivity(Intent(this@MainActivity,VideoActivity::class.java))
        }
    }
}
